package com.SNQ.project.English_speaking_learning_system.controller.user;

import com.SNQ.project.English_speaking_learning_system.common.result.Result;
import com.SNQ.project.English_speaking_learning_system.pojo.vo.UserVO;
import com.SNQ.project.English_speaking_learning_system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/rank")
@Api(tags = "排行榜相关接口")
@Slf4j
public class RankController {

    @Autowired
    private UserService userService;

    @GetMapping("/article/checkin")
    @ApiOperation("获取用户打卡次数 Top 10 排行")
    public Result<List<UserVO>> getArticleCheckinRanking() {
        List<UserVO> list = userService.getArticleByCheckin();
        return Result.success(list);
    }

    @GetMapping("/article/scores")
    @ApiOperation("获取用户总分数 Top 10 排行")
    public Result<List<UserVO>> getArticleScoresRanking() {
        List<UserVO> list = userService.getArticleByScores();
        return Result.success(list);
    }

    @GetMapping("/department/{rankType}/{department}")
    @ApiOperation("获取院系排行 Top 10")
    public Result<List<UserVO>> getDepartmentRanking(
            @PathVariable("rankType") String rankType,
            @PathVariable("department") String department) {
        List<UserVO> list;
        if ("checkin".equals(rankType)) {
            // 按打卡次数排行
            list = userService.getDepartmentByCheckin(department);
        } else if ("scores".equals(rankType)) {
            // 按总分排行
            list = userService.getDepartmentByScores(department);
        } else {
            return Result.error("Invalid rank type");
        }
        return Result.success(list);
    }

    @GetMapping("/major/{rankType}/{department}/{major}")
    @ApiOperation("获取专业排行 Top 10")
    public Result<List<UserVO>> getMajorRanking(
            @PathVariable("rankType") String rankType,
            @PathVariable("department") String department,
            @PathVariable("major") String major) {
        List<UserVO> list;
        if ("checkin".equals(rankType)) {
            // 按打卡次数排行
            list = userService.getMajorByCheckin(department, major);
        } else if ("scores".equals(rankType)) {
            // 按总分排行
            list = userService.getMajorByScores(department, major);
        } else {
            return Result.error("Invalid rank type");
        }
        return Result.success(list);
    }

    @GetMapping("/classNumber/{rankType}/{department}/{major}/{classNumber}")
    @ApiOperation("获取班级排行 Top 10")
    public Result<List<UserVO>> getClassNumberRanking(
            @PathVariable("rankType") String rankType,
            @PathVariable("department") String department,
            @PathVariable("major") String major,
            @PathVariable("classNumber") String classNumber) {
        List<UserVO> list;
        if ("checkin".equals(rankType)) {
            // 按打卡次数排行
            list = userService.getClassNumberByCheckin(department, major, classNumber);
        } else if ("scores".equals(rankType)) {
            // 按总分排行
            list = userService.getClassNumberByScores(department, major, classNumber);
        } else {
            return Result.error("Invalid rank type");
        }
        return Result.success(list);
    }

}
